<template>
	<view style="padding: 30rpx;">
		<button @click="onSetTimeCall()">单次的定时器setTimeout</button>
		<button @click="onSetTimeCallxy(userName,password)">带参数的单次的定时器setTimeout</button>
		<button @click="onTimeoutClear()" type="warn">取消定时器</button>
		<button @click="onInterval()" type="primary">周期打印</button>
		<button @click="onClearInter()" type="warn" >取消周期打印</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName:'YXP',
				password:'666',
				timeoutID:null,
				count:0,
				intervalID:null
			}
		},
		methods: {
			onSetTimeCall(){
				console.log("onSetTimeCall-->");
				//setTimeout(callback,ms,any[])
				//箭头函数（匿名函数）（）=>{  }
				this.timeoutID = setTimeout(()=>{
					//延时之后要执行的代码
					console.log("我延时2秒才会打印");
				},2000)
			},
			onSetTimeCallxy(name,pwd){
				console.log("onSetTimeCallxy--> name:"+name+",pwd:"+pwd);
				//console.log("onSetTimeCallxy--> name:"+this.userName+",pwd:"+this.password);
				setTimeout((x,y)=>{
					console.log("我可以传参数了> x:"+x+",y:"+y);
				},2000,name,pwd)
			},
			onTimeoutClear(){
				console.log("onTimeoutClear-->定时器被我取消了:"+this.timeoutID);
				clearTimeout(this.timeoutID)
			},
			onInterval(){
				if (this.intervalID) clearInterval(this.intervalID);
				this.intervalID = setInterval(()=>{
					this.count++;
					console.log("周期打印 count:",this.count);
				},2000);
			},
			onClearInter(){
				if (this.intervalID){
				console.log("onClearInter-->周期打印被取消了：",this.intervalID);
				clearInterval(this.intervalID);
				this.intervalID = null;
				}
			}
		}
	}
</script>

<style>
button{
	margin: 30rpx;
}
</style>
